package com.xxx.dorm.dao.impl;

import com.xxx.dorm.dao.DormitoryDao;
import com.xxx.dorm.domain.Building;
import com.xxx.dorm.domain.Dormitory;
import com.xxx.dorm.domain.Student;
import com.xxx.dorm.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class DormitoryDaoImpl implements DormitoryDao {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 通过寝室号id查找寝室信息
     * @param did
     * @return
     */
    @Override
    public Dormitory findByDid(Integer did) {
        String sql;
        sql = "SELECT * FROM t_dormitory WHERE id = ? ORDER BY bid";
        return template.queryForObject(sql,new BeanPropertyRowMapper<>(Dormitory.class),did);
    }

    @Override
    public List<Dormitory> findAll() {
        String sql;
        sql = "SELECT * FROM t_dormitory";
        return template.query(sql,new BeanPropertyRowMapper<>(Dormitory.class));
    }

    @Override
    public List<Dormitory> findDormByBuildName(String buildName) {
        String sql;
        sql = "SELECT * FROM t_dormitory where t_dormitory.bid in (SELECT t_building.id FROM t_building WHERE t_building.name = ?);";
        return template.query(sql,new BeanPropertyRowMapper<>(Dormitory.class),buildName);
    }

    @Override
    public List<Dormitory> find(String conditions, String information) {
        String sql = null;
        if(conditions.equals("number")){
            sql = "select * from t_dormitory where number = ?";
        }else if(conditions.equals("buildName")){
            sql = "SELECT * from t_dormitory WHERE t_dormitory.bid = (SELECT id FROM t_building WHERE t_building.name = ?)";
        }else if(conditions.equals("buildName_number")){
            System.out.println(information.split("#")[0]);
            System.out.println(information.split("#")[1]);
            //判断是否已存在宿舍（添加宿舍时用）
            sql = "SELECT * FROM t_dormitory WHERE t_dormitory.number = ? AND t_dormitory.bid = ?";
            return template.query(sql,new BeanPropertyRowMapper<>(Dormitory.class),information.split("#")[0]+"",information.split("#")[1]+"");
        }else if(conditions.equals("buildingname_dormname")){
            String name = information.split("#")[0]+"";
            String number = information.split("#")[1]+"";
            //查找宿舍楼和寝室号时用
            sql = "select t_dormitory.id,t_dormitory.number,t_dormitory.capablePeople,t_dormitory.actualPeople,t_dormitory.bid from t_dormitory,t_building where" +
                    " t_dormitory.bid = t_building.id and t_building.name = ? and t_dormitory.number = ?";
            return template.query(sql,new BeanPropertyRowMapper<>(Dormitory.class),name,number);
        }
        return template.query(sql,new BeanPropertyRowMapper<>(Dormitory.class),information+"");
    }

    @Override
    public void addDormitory(Dormitory dormitory) {
        String sql = null;
        sql = "insert into t_dormitory(number ,capablePeople,actualPeople,bid) values (?,?,?,?)";
        template.update(sql,dormitory.getNumber(),dormitory.getCapablePeople(),dormitory.getActualPeople(),dormitory.getBid());
    }

    /**
     * 删除寝室通过bid
     * @param bid
     */
    @Override
    public void deleteDormByBid(Integer bid) {
        String sql = null;
        sql = "delete from t_dormitory where bid = ?";
        template.update(sql,bid);
    }

    @Override
    public void updateDorm(Dormitory dormitory) {
        String sql = null;
        sql = "update t_dormitory set number = ? where id = ?";
        template.update(sql,dormitory.getNumber(),dormitory.getId());
    }

    /**
     * 学生增加修改宿舍实际人数
     * @param did
     */
    @Override
    public void addActualPeople(Integer did) {
        String sql = null;
        sql = "SELECT actualPeople FROM t_dormitory WHERE t_dormitory.id = ?";
        Integer oldnum = template.queryForObject(sql,Integer.class,did);
        sql = "UPDATE t_dormitory set actualPeople = ? WHERE t_dormitory.id = ?";
        String actualPeople = String.valueOf(Integer.valueOf(oldnum)+1);
        template.update(sql,actualPeople,did);
    }
    /**
     * 学生删除修改宿舍实际人数
     * @param did
     */
    @Override
    public void reduceActualPeople(Integer did) {
        String sql = null;
        sql = "SELECT actualPeople FROM t_dormitory WHERE t_dormitory.id = ?";
        Integer oldnum = template.queryForObject(sql,Integer.class,did);
        sql = "UPDATE t_dormitory set actualPeople = ? WHERE t_dormitory.id = ?";
        String actualPeople = String.valueOf(Integer.valueOf(oldnum)-1);
        template.update(sql,actualPeople,did);
    }
}
